package com.xzg.user.controller;

import com.xzg.common.constants.Constant;
import com.xzg.common.page.PageQueryBean;
import com.xzg.common.resp.APIResult;
import com.xzg.common.vo.QueryCondition;
import com.xzg.middleTable.entity.Label;
import com.xzg.user.entity.User;
import com.xzg.user.service.UserService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     *@Author Ye
     *Date 2018-03-02 15:31
     *@Description 用户登录
     */
    @RequestMapping(method= RequestMethod.POST ,value= "/checkAPP")
    @ResponseBody
    public APIResult loginAPP(@RequestBody  User user ) throws UnsupportedEncodingException, NoSuchAlgorithmException {
//        String username = request.getParameter("username");
//        String pwd = request.getParameter("password");
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
        if(("yes").equals(user.getActive())) {
            token.setRememberMe(true);
        }
        Subject subject = SecurityUtils.getSubject();
        APIResult resp =  new APIResult();
        SecurityUtils.getSubject().getSession().setAttribute("cid",user.getCID());
        try{
            subject.login(token);
            //设置Session的过期时间
            SecurityUtils.getSubject().getSession().setTimeout(1800000);
             user =(User) SecurityUtils.getSubject().getSession().getAttribute("userinfo");
             resp.setData(user);
        }catch (Exception e) {
            resp.setCode(Constant.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("登录失败");
        }
        return resp;
    }

    /**
     *@Author Ye
     *Date 2018-03-07 13:39
     *@Description 网站登录入口
     */
    @RequestMapping(method= RequestMethod.POST ,value= "/check")
    @ResponseBody
    public String login( HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String username = request.getParameter("username");
        String pwd = request.getParameter("password");
        UsernamePasswordToken token = new UsernamePasswordToken(username,pwd);

        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(token);
            //设置Session的过期时间
            SecurityUtils.getSubject().getSession().setTimeout(1800000);
        }catch (Exception e) {
            return "login_fail";
        }
        return "login_succ";
    }


    /**
     *@Author Ye
     *Date 2018-03-02 15:31
     *@Description 用户注册
     */
    @RequestMapping("/register")
    @ResponseBody
    public void register(@RequestBody User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        userService.creatUser(user);
    }

    /**
     *@Author Ye
     *Date 2018-03-03 15:14
     *@Description 查询所有员工
     */
    @RequestMapping("/all")
    @ResponseBody
    public PageQueryBean findAll(QueryCondition condition) {
        PageQueryBean result = userService.findAllUser(condition);
        return result;
    }

    /**
     *@Author Ye
     *Date 2018-03-05 16:20
     *@Description 跳转到登录页
     */
    @RequestMapping("/login")
    public String main() {
        return "index";
    }

    /**
     *@Author Ye
     *Date 2018-03-05 16:20
     *@Description 进入主页
     */
    @RequestMapping("/home")
    public String home(){
        return "home";
    }


    @RequestMapping("/allUser")
    public String allUser() {
        return "allUser";
    }

    /**
     *@Author Ye
     *Date 2018-03-05 16:21
     *@Description JS获取当前用户信息
     */
    @RequestMapping("/userinfo")
    @ResponseBody
    public User userinfo() {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("userinfo");
        return user;
    }

    @RequestMapping("/appuserinfo")
    @ResponseBody
    public User appuserinfo() {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("userinfo");
//        User user = new User();
//        user.setUsername("xiaoshu");
//        user.setId(2l);
        user = userService.checkCid(user);
        return user;
    }

    @RequestMapping("/wuserinfo")
    @ResponseBody
    public APIResult wuserinfo(User user) {
//        User user = new User();
//        user.setUsername("xiaoshu");
//        user.setId(2l);
        APIResult resp = userService.checklabel(user);
        return resp;
    }

    @RequestMapping("/loginOut")
    @ResponseBody
    public void loginOut() {
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
            subject.logout();

        }
    }
    @RequestMapping("/modifyNickName")
    @ResponseBody
    public APIResult modifyNickname(@RequestBody User user) throws InterruptedException {
        APIResult resp = new APIResult();
        int i = userService.updateUser(user);
        if(i == 1) {
            resp.setMessage("修改成功");
        } else {
            resp.setMessage("修改失败");
            resp.setCode(500);
        }
        return resp;
    }


    @RequestMapping("/insertLabel")
    @ResponseBody
    public APIResult insertLabel(@RequestBody String labelName) {
        JSONObject json = JSONObject.fromObject(labelName);
        Label label = new Label();
        label.setLabelName(json.getString("labelName"));
        APIResult resp = userService.insertLabel(label);
        return resp;
    }

    @RequestMapping("/updateLabelUser")
    @ResponseBody
    public APIResult updateLabelUser(@RequestBody QueryCondition condition) {
        APIResult resp = userService.updateLabelUser(condition);
        return resp;
    }

    @RequestMapping("deleteLabel")
    @ResponseBody
    public APIResult deleteLabel(@RequestBody QueryCondition condition) {
        APIResult resp = userService.deleteLabel(condition);
        return resp;
    }


    @RequestMapping("/allLabel")
    @ResponseBody
    public APIResult allLbel() {
        APIResult resp = userService.allLabel();
        return resp;
    }



//    @RequestMapping("/uploadImage")
//    @ResponseBody
//    public APIResult uploadImage() {
//
//    }



}
